package com.umpay.service.consum.impl;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.umpay.dao.offline.TransOffDao;
import com.umpay.dao.offline.consume.ConsumOrderOffDao;
import com.umpay.dao.online.TransDao;
import com.umpay.dao.online.consum.ConsumOrderOnlineDao;
import com.umpay.entity.Trans;
import com.umpay.entity.consum.ConsumOrder;
import com.umpay.service.consum.ConsumeQueryService;
import com.umpay.utils.TableNameHelper;

@Service
public class ConsumeQueryServiceImpl implements ConsumeQueryService {

	private static final Logger log = LogManager.getLogger(ConsumeQueryServiceImpl.class);
	
	@Autowired
	private ConsumOrderOffDao consumOrderOffDao;
	
	@Autowired
	private ConsumOrderOnlineDao consumOrderOnlineDao;
	
	@Autowired
	private TableNameHelper tableNameHelper;
	
	@Autowired
	private TransDao transDao;
	
	@Autowired
	private TransOffDao transOffDao;
	
	@Override
	public ConsumOrder findConsumeOrderOff(ConsumOrder order) {
		log.info("【查询消费订单离线表请求参数】"+order);
		ConsumOrder consumOrder = null;
		if(StringUtils.isEmpty(order.getTableName())){
			tableNameHelper.generateOfflineTableName(order, order.getOrderDate());
		}
		try {
			consumOrder = consumOrderOffDao.find(order);
		} catch (DataAccessException e) {
			log.error("【查询消费订单离线表信息】查询ConsumeOrder失败",e);
		}
		log.info("【查询消费订单离线表响应结果】"+consumOrder);
		return consumOrder;
	}
	
	@Override
	public ConsumOrder findConsumeOrderOnline(ConsumOrder order) {
		log.info("【查询消费订单在线表请求参数】"+order);
		ConsumOrder consumOrder = null;
		try {
			tableNameHelper.generateTableName(order,order.getOrderDate());
			consumOrder = consumOrderOnlineDao.find(order);
		} catch (DataAccessException e) {
			log.error("【查询消费订单在线表信息】查询ConsumeOrder失败",e);
		}
		log.info("【查询消费订单在线表响应结果】"+consumOrder);
		return consumOrder;
	}

	@Override
	public Trans findConsumeTransOff(Trans queryTrans) {
		log.info("【查询消费交易离线表请求参数】"+queryTrans);
		Trans trans = null;
		try {
			tableNameHelper.generateOfflineTableName(queryTrans, queryTrans.getOrderDate());
			trans = transOffDao.find(queryTrans);
		} catch (DataAccessException e) {
			log.error("【查询消费交易离线表信息】查询trans失败",e);
		}
		log.info("【查询消费交易离线表响应结果】"+trans);
		return trans;
	}

	@Override
	public Trans findConsumeTransOnline(Trans queryTrans) {
		log.info("【查询消费交易在线表请求参数】"+queryTrans);
		Trans trans = null;
		try {
			tableNameHelper.generateTableName(queryTrans, queryTrans.getOrderDate());
			trans = transDao.find(queryTrans);
		} catch (DataAccessException e) {
			log.error("【查询消费交易在线表信息】查询trans失败",e);
		}
		log.info("【查询消费交易在线表响应结果】"+trans);
		return trans;
	}
}
